
import 'package:flutter_test/flutter_test.dart';
import 'package:flutter/services.dart';
import 'package:flutter_vibrate_ohos/flutter_vibrate_ohos.dart';

void main() {
  TestWidgetsFlutterBinding.ensureInitialized();
  const MethodChannel channel = MethodChannel('vibrate');
  late MethodChannel mockChannel;

  setUp(() {
    mockChannel = MethodChannel('vibrate');
    channel.setMockMethodCallHandler((MethodCall methodCall) async {
      if (methodCall.method == 'vibrate') {
        return null;
      } else if (methodCall.method == 'canVibrate') {
        return true;
      } else if (methodCall.method == 'impact' ||
          methodCall.method == 'error' ||
          methodCall.method == 'success' ||
          methodCall.method == 'warning' ||
          methodCall.method == 'selection' ||
          methodCall.method == 'heavy' ||
          methodCall.method == 'medium' ||
          methodCall.method == 'light') {
        return null;
      }
      return null;
    });
  });

  tearDown(() {
    channel.setMockMethodCallHandler(null);
  });

  test('vibrate method calls the native vibrate method', () async {
    await Vibrate.vibrate();
    expect(mockChannel, isNotNull);
  });

  test('canVibrate returns true if the device can vibrate', () async {
    final bool canVibrate = await Vibrate.canVibrate;
    expect(canVibrate, isTrue);
  });

  test('feedback method calls the corresponding native method', () {
    for (final type in FeedbackType.values) {
      Vibrate.feedback(type);
    }
    expect(mockChannel, isNotNull);
  });

  test('vibrateWithPauses method calls vibrate and pauses correctly', () async {
    await Vibrate.vibrateWithPauses(const [Duration(milliseconds: 100), Duration(milliseconds: 200)]);
    expect(mockChannel, isNotNull);
  });
}